package com.ehome.poct.core.dao.mybatis;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ehome.mybatisplus.support.AbstractCrudRepository;
import com.ehome.poct.core.api.dto.report.TestConfPoctUniAnalysisDTO;
import com.ehome.poct.core.api.vo.checkdata.TestDataDetailVO;
import com.ehome.poct.core.dao.entity.check.TestConfPoctUniAnalysisDO;
import com.ehome.poct.core.dao.mybatis.mapper.TestConfPoctUniAnalysisMapper;
import com.ehome.poct.core.repository.report.TestConfPoctUniAnalysisRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.Arrays;
import java.util.List;

/**
 * 金标仪联合检测分析判读配置表RepositoryImpl
 *
 * @author muqiu
 * @Date 2022-10-18 13:57
 */
@Repository
public class TestConfPoctUniAnalysisRepositoryImpl extends AbstractCrudRepository<TestConfPoctUniAnalysisDO, TestConfPoctUniAnalysisDTO> implements TestConfPoctUniAnalysisRepository {

    @Autowired private TestConfPoctUniAnalysisMapper testConfPoctUniAnalysisMapper;
    @Override
    public TestConfPoctUniAnalysisDTO getAnalysisByData(TestDataDetailVO dataA, TestDataDetailVO dataB, Integer isChild) {
        List<TestConfPoctUniAnalysisDO> analysisList;
        if(dataA.getStripTypeCode()==4 && dataB.getStripTypeCode()==5) {
            analysisList = this.testConfPoctUniAnalysisMapper.selectList(Wrappers.<TestConfPoctUniAnalysisDO>lambdaQuery()
                    .eq(TestConfPoctUniAnalysisDO::getStripTypeCodeA, dataA.getStripTypeCode())
                    .lt(TestConfPoctUniAnalysisDO::getStripTypeAMin, dataA.getValue1())
                    .ge(TestConfPoctUniAnalysisDO::getStripTypeAMax, dataA.getValue1())
                    .eq(TestConfPoctUniAnalysisDO::getStripTypeCodeB, dataB.getStripTypeCode())
                    .lt(TestConfPoctUniAnalysisDO::getStripTypeBMin, dataB.getValue1())
                    .ge(TestConfPoctUniAnalysisDO::getStripTypeBMax, dataB.getValue1())
                    .eq(TestConfPoctUniAnalysisDO::getPatientType, isChild)
            );
        }else
        {
            analysisList = this.testConfPoctUniAnalysisMapper.selectList(Wrappers.<TestConfPoctUniAnalysisDO>lambdaQuery()
                    .eq(TestConfPoctUniAnalysisDO::getStripTypeCodeA, dataA.getStripTypeCode())
                    .le(TestConfPoctUniAnalysisDO::getStripTypeAMin, dataA.getValue1())
                    .gt(TestConfPoctUniAnalysisDO::getStripTypeAMax, dataA.getValue1())
                    .eq(TestConfPoctUniAnalysisDO::getStripTypeCodeB, dataB.getStripTypeCode())
                    .le(TestConfPoctUniAnalysisDO::getStripTypeBMin, dataB.getValue1())
                    .gt(TestConfPoctUniAnalysisDO::getStripTypeBMax, dataB.getValue1())
                    .eq(TestConfPoctUniAnalysisDO::getPatientType, isChild )
            );
        }
        // 通过试纸批次过滤解读
        return BeanUtil.toBean(analysisList.parallelStream()
                .filter(i -> CollUtil.contains(Arrays.asList(i.getStripTypeABatch().split(",")), dataA.getStripBatch().toString()))
                .filter(i -> CollUtil.contains(Arrays.asList(i.getStripTypeBBatch().split(",")), dataB.getStripBatch().toString()))
                .findFirst().orElse(null), TestConfPoctUniAnalysisDTO.class);
    }
}
